package com.jnu.textbook;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class EditBookActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_book);

        Intent intent = getIntent();
        int position = intent.getIntExtra("position",0);

        //统一处理由MainActivity传进来的数据
        EditText editTextTitle = findViewById(R.id.edit_book_title);
        String title = intent.getStringExtra("title");//取数据
        if(null!=title){//title为空，说明是添加数据
            editTextTitle.setText(title);
        }

        Button buttonOK = this.findViewById(R.id.button_ok);
        buttonOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.putExtra("position",position);
                intent.putExtra("title",editTextTitle.getText().toString());

                setResult(BookFragment.RESULT_CODE_ADD_DATA,intent);

                EditBookActivity.this.finish();
            }
        });
        Button buttonCancel = this.findViewById(R.id.button_cancel);
        buttonCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditBookActivity.this.finish();
            }
        });
    }
}